package com.mall.service.impl;

import com.mall.mapper.GoodsMapper;
import com.mall.service.GoodsService;
import com.mall.util.PageUtils;
import com.mall.util.StringUtil;
import com.mall.entity.Goods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author:lck
 * @Date 2024/1/15 16:15
 */

@Service
public class GoodsServiceImpl implements GoodsService {
    private GoodsMapper goodsMapper;

    @Autowired
    public GoodsServiceImpl(GoodsMapper goodsMapper){
        this.goodsMapper = goodsMapper;
    }

    @Override
    public PageUtils<Goods> getGoodsList(Integer pageNow, Integer pageSize) {

        ArrayList<Goods> goods = goodsMapper.selectGoods((pageNow-1)*pageSize, pageSize);
        long counts = goodsMapper.selectCounts();
        PageUtils pages = new PageUtils<Goods>(goods,pageSize,pageNow,counts);
        return pages;
    }

    @Override
    public int delistGoods(ArrayList<String> goodsId) {
        return goodsMapper.delistGoods(goodsId);
    }

    @Override
    public int getDelistNum(ArrayList<String> goodsId) {
        return goodsMapper.getDelistNum(goodsId);
    }

    @Override
    public int listGoods(ArrayList<String> goodsId) {
        return goodsMapper.listGoods(goodsId);
    }

    @Override
    public int addGoods(Goods goods) {
        //随机生成8位的字符串
        String id = StringUtil.getUUIDRandomString(8);
        goods.setGoodsId(id);
        System.out.println(goods);
        System.out.println(id);
        Goods goods1 = goodsMapper.selectGoodByGoodsId(id);
        System.out.println(goods1);
        return goodsMapper.addGoods(goods);
    }

    @Override
    public Goods getGoodsById(String goods_id) {
        Goods good = goodsMapper.getGoodsById(goods_id);
        return good;
    }

    @Override
    public PageUtils<Goods> getGoodsByLike(String keyword, Integer currentPage, Integer pageSize) {
        ArrayList<Goods> goods = goodsMapper.getGoodsByLike(keyword, (currentPage-1)*pageSize, pageSize);
        System.out.println(goods);
        int counts = goodsMapper.getGoodsCountsByLike(keyword);
        PageUtils pages = new PageUtils<Goods>(goods,pageSize,currentPage,counts);
        return pages;
    }

}
